/**
 * 
 */
package ads.engenharia.bl;

import java.sql.SQLException;
import java.util.ArrayList;

import ads.engenharia.dal.ProfessorCompetenceDAO;
import ads.engenharia.dal.ProfessorDAO;
import ads.engenharia.dal.ProfessorScheduleDAO;
import ads.engenharia.entity.Discipline;
import ads.engenharia.entity.Professor;
import ads.engenharia.entity.Schedule;

/**
 * @author Lanedraex
 *
 */
public class ProfessorBO {

	private ProfessorDAO _professorDAO;
	private ProfessorCompetenceDAO _professorCompetenceDAO;
	private ProfessorScheduleDAO _professorScheduleDAO;

	public ProfessorBO() {

		_professorDAO = new ProfessorDAO();
		_professorScheduleDAO = new ProfessorScheduleDAO();
		_professorCompetenceDAO = new ProfessorCompetenceDAO();
	}

	/**
	 * 
	 * @param professor
	 * @throws SQLException
	 */
	public void save(Professor professor) throws SQLException {

		professor = _professorDAO.save(professor);
		_professorCompetenceDAO.save(professor);
		_professorScheduleDAO.save(professor);

	}

	/**
	 * 
	 * @param professor
	 * @throws SQLException
	 */
	public void delete(Professor professor) throws SQLException {

		_professorDAO.delete(professor);

	}

	/**
	 * 
	 * @throws SQLException
	 */
	public void deleteAll() throws SQLException {

		_professorDAO.deleteAll();

	}

	/**
	 * 
	 * @param professor
	 * @throws SQLException
	 */
	public void update(Professor professor) throws SQLException {

		_professorDAO.update(professor);
		_professorScheduleDAO.update(professor);
		_professorCompetenceDAO.update(professor);

	}

	/**
	 * 
	 * @param professor
	 * @return
	 * @throws SQLException
	 */
	public Professor search(Professor professor) throws SQLException {

		professor = _professorDAO.search(professor);

		// Procura pelo schedule para completar o professor
		professor.schedule(searchProfessorSchedule(professor));

		// Procura pelas competencias para completar o professor
		professor.competence(searchProfessorCompetence(professor));

		return professor;

	}

	/**
	 * 
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Professor> searchAll() throws SQLException {

		return _professorDAO.searchAll();

	}

	/**
	 * 
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public Professor searchById(int id) throws SQLException {

		return _professorDAO.searchById(id);

	}

	/**
	 * 
	 * @param name
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Professor> searchByName(String name) throws SQLException {

		ArrayList<Professor> professors;
		professors = _professorDAO.searchByName(name);

		for (Professor p : professors) {
			// Procura pelo schedule para completar o professor
			p.schedule(searchProfessorSchedule(p));

			// Procura pelas competencias para completar o professor
			p.competence(searchProfessorCompetence(p));
		}

		return professors;

	}

	/**
	 * 
	 * @param competence
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Professor> searchByCompetence(Discipline competence)
			throws SQLException {

		ArrayList<Professor> professors;
		professors = _professorDAO.searchByCompetence(competence);

		for (Professor p : professors) {
			// Procura pelo schedule para completar o professor
			p.schedule(searchProfessorSchedule(p));

			// Procura pelas competencias para completar o professor
			p.competence(searchProfessorCompetence(p));
		}

		return professors;

	}

	/**
	 * 
	 * @param status
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Professor> searchByStatus(boolean status)
			throws SQLException {

		ArrayList<Professor> professors;
		professors = _professorDAO.searchByStatus(status);

		for (Professor p : professors) {
			// Procura pelo schedule para completar o professor
			p.schedule(searchProfessorSchedule(p));

			// Procura pelas competencias para completar o professor
			p.competence(searchProfessorCompetence(p));
		}

		return professors;

	}

	/**
	 * 
	 * @param day
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Professor> searchByDay(Schedule.Day day)
			throws SQLException {

		ArrayList<Professor> professors;
		professors = _professorDAO.searchByDay(day);

		for (Professor p : professors) {
			// Procura pelo schedule para completar o professor
			p.schedule(searchProfessorSchedule(p));

			// Procura pelas competencias para completar o professor
			p.competence(searchProfessorCompetence(p));
		}

		return professors;
	}

	/**
	 * 
	 * @param time
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Professor> searchByTime(Schedule.Time time)
			throws SQLException {

		ArrayList<Professor> professors;
		professors = _professorDAO.searchByTime(time);

		for (Professor p : professors) {
			// Procura pelo schedule para completar o professor
			p.schedule(searchProfessorSchedule(p));

			// Procura pelas competencias para completar o professor
			p.competence(searchProfessorCompetence(p));
		}

		return professors;
	}

	/**
	 * 
	 * @param hour
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Professor> searchByHour(Schedule.Hour hour)
			throws SQLException {

		ArrayList<Professor> professors;
		professors = _professorDAO.searchByHour(hour);

		for (Professor p : professors) {
			// Procura pelo schedule para completar o professor
			p.schedule(searchProfessorSchedule(p));

			// Procura pelas competencias para completar o professor
			p.competence(searchProfessorCompetence(p));
		}

		return professors;
	}

	/**
	 * 
	 * @param professor
	 * @return
	 * @throws SQLException
	 */
	private Schedule searchProfessorSchedule(Professor professor)
			throws SQLException {

		return _professorScheduleDAO.searchProfessorSchedule(professor);
	}

	/**
	 * 
	 * @param professor
	 * @return
	 * @throws SQLException
	 */
	private ArrayList<Discipline> searchProfessorCompetence(Professor professor)
			throws SQLException {

		return _professorCompetenceDAO.searchProfessorCompetence(professor);
	}
}
